﻿using System;
using System.Collections.Generic;
using System.Linq;
using OrderingPlatform.Infrastructure;
using OrderingPlatform.Model.ProductManage;
using OrderingPlatform.MvcExtension;
using OrderingPlatform.MvcPager.Mvc;

namespace OrderingPlatform.DalService.ProductManage
{
    public class ProductDal
    {
        /// <summary>
        /// 获取基本model
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="hasAddVisist"></param>
        /// <returns></returns>
        public Product GetModel(Guid productId,bool hasAddVisist=false)
        {
            using (var db = new OrderingPlatformDbEntities())
            {
                var model= db.Products.FirstOrDefault(a => a.ProductId == productId);
                if (!hasAddVisist) return model;
                if (model == null) return new Product();
                model.VistCount =( model.VistCount ?? 0) + 1;
                db.SaveChanges();
                return model;
            }
        }
        /// <summary>
        /// 新增或者修改
        /// </summary>
        /// <param name="model"></param>
        public void AddorEdit(Product model)
        {
            var enty = new Product();
            using (var db = new OrderingPlatformDbEntities())
            {
                if (model.ProductId == Guid.Empty) //新增
                {
                    var userId = UserManage.UserDal.GetCurrentUserId();
                    AutoMapperManage.Map(model, enty);
                    enty.State = true;
                    enty.ProductId = Guid.NewGuid();
                    enty.CreateDate = DateTime.Now;
                    enty.VistCount = 0;
                    enty.UserId = userId;
                    db.Products.Add(enty);

                }
                else //修改
                {
                    enty = db.Products.FirstOrDefault(p => p.ProductId == model.ProductId);
                    if (enty != null)
                    {
                        var createDate = enty.CreateDate;
                        var userId = enty.UserId;
                        var visit = enty.VistCount;
                        AutoMapperManage.Map(model, enty);
                        enty.CreateDate = createDate;
                        enty.State = true;
                        enty.UpdateDate = DateTime.Now;
                        enty.UserId = userId;
                        enty.VistCount = visit;
                    }
                    
                }
                
                db.SaveChanges();

            }
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PagedList<ProductModel> GetList(ProductModel model, int pageIndex, int pageSize)
        {
            using (var db = new OrderingPlatformDbEntities())
            {
                var reg = db.Products.Where(a => a.State == true);
                if (model.UserId != null)
                {
                    reg = reg.Where(a => a.UserId == model.UserId);
                }
                if (!string.IsNullOrEmpty(model.ProductName))
                {
                    reg = reg.Where(a => a.ProductName.Contains(model.ProductName));
                }
                if (!string.IsNullOrEmpty(model.ProductType))
                {
                    reg = reg.Where(a => a.ProductType == model.ProductType);
                }
                if (model.HasSell != null)
                {
                    reg = reg.Where(a => a.HasSell == model.HasSell);
                }
                if (model.PriceBegin != null)
                {
                    reg = reg.Where(a => a.CurrentPrice >= model.PriceBegin);
                }
                if (model.PriceEnd != null)
                {
                    reg = reg.Where(a => a.CurrentPrice <= model.PriceEnd);
                }
                if (model.CreateBegin != null)
                {
                    reg = reg.Where(a => a.CreateDate >= model.CreateBegin);
                }
                if (model.CreateEnd != null)
                {
                    var d = model.CreateEnd.Value.AddDays(1);
                    reg = reg.Where(a => a.CreateDate < d);
                }
                return null;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        public void Del(string ids)
        {
            var gids = ids.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).Select(Guid.Parse);
            using (var db = new OrderingPlatformDbEntities())
            {

                var reg = db.Products.Where(a => gids.Contains(a.ProductId));
                foreach (var item in reg)
                {
                    item.State = false;
                }
                db.SaveChanges();
            }
        }
        
    }
}
